package com.daon.service.impl;

import com.daon.pojo.SysNode;
import com.daon.service.ITreeService;
import com.daon.vo.SysRoleNodeVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 1daon
 * @EMAIL 532418116@qq.com
 * @Version 创建时间：2019/1/30 5:07 PM
 * @Modified By
 */
@Service("iTreeService")
public class TreeServiceImpl implements ITreeService {

    /**
     * 获取节点树形结构多维数组
     * @param sysNode
     * @return
     */
    @Override
    public List<Object> nodeList2Tree(List<SysNode> sysNode) {
        List<Object> sysNodeTree = nodeTreeArrayList(sysNode);
        return sysNodeTree;
    }

    /**
     * 获取节点menu结构多维数组
     * @param sysRoleNodeVos
     * @return
     */
    @Override
    public List<Object> roleNodeList2TreeArray(List<SysRoleNodeVo> sysRoleNodeVos) {
        List<Object> sysNodeTree = roleNodeList(sysRoleNodeVos);
        return sysNodeTree;
    }

    /**
     * 获取节点menu结构多维数组
     * @param sysNodeList
     * @return
     */
    @Override
    public List<Object> nodeMenu2TreeArray(List<SysNode> sysNodeList) {
        List<Object> sysNodeTree = nodeMenuList(sysNodeList);
        return sysNodeTree;
    }

    private List<Object> nodeTreeArrayList(List<SysNode> menu){
        List<SysNode> menuCommon = menu;
        List<Object> list = new ArrayList<Object>();
        Map<String, String> pidAndLevelMap = new LinkedHashMap<>();
        for (SysNode x : menu) {
            Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
            if(x.getNodePid() == 0){
                int level = 1;
                mapArr.put("nodeId", x.getNodeId());
                mapArr.put("level", level);
                mapArr.put("nodeName", x.getNodeName());
                mapArr.put("nodeIcon", x.getNodeIcon());
                mapArr.put("nodeSort", x.getNodeSort());
                mapArr.put("nodeType", x.getNodeType());
                mapArr.put("nodeDesc", x.getNodeDesc());
                mapArr.put("nodePid", x.getNodePid());
                mapArr.put("nodeUrl", x.getNodeUrl());
                mapArr.put("nodeTopPid", x.getNodeTopPid());
                list.add(mapArr);
                pidAndLevelMap.put(String.valueOf(x.getNodeId()),String.valueOf(level));
                treeNodeChild(x.getNodeId(), list , menuCommon, pidAndLevelMap);
            }
        }
        return list;
    }

    private void treeNodeChild(Integer id, List<Object> list, List<SysNode> menuCommon, Map<String, String> pidAndLevelMap){
        for(SysNode a:menuCommon){
            Map<String,Object> childArray = new LinkedHashMap<String, Object>();
            if(a.getNodePid() == id){
                int level = Integer.parseInt(pidAndLevelMap.get(String.valueOf(a.getNodePid())));
                childArray.put("nodeId", a.getNodeId());
                childArray.put("level", ++level);
                childArray.put("nodeName", a.getNodeName());
                childArray.put("nodeIcon", a.getNodeIcon());
                childArray.put("nodeSort", a.getNodeSort());
                childArray.put("nodeType", a.getNodeType());
                childArray.put("nodeDesc", a.getNodeDesc());
                childArray.put("nodePid", a.getNodePid());
                childArray.put("nodeUrl", a.getNodeUrl());
                childArray.put("nodeTopPid", a.getNodeTopPid());
                list.add(childArray);
                pidAndLevelMap.put(String.valueOf(a.getNodeId()),String.valueOf(level));
                treeNodeChild(a.getNodeId(), list , menuCommon, pidAndLevelMap);
            }
        }
    }

    /**
     * 获取角色对应节点的树形结构
     * @param sysRoleNodeVos
     * @return [
     *  'id'=>'',
     *  'pid'=>'',
     *  'childList'=>[]
     */
    private List<Object> roleNodeList(List<SysRoleNodeVo> sysRoleNodeVos){
        List<SysRoleNodeVo> menuCommon = sysRoleNodeVos;
        List<Object> list = new ArrayList<Object>();
        int level = 1;
        for (SysRoleNodeVo x : sysRoleNodeVos) {
            Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
            if(x.getSysNode().getNodePid() == 0){
                mapArr.put("nodeId", x.getSysNode().getNodeId());
                mapArr.put("nodeName", x.getSysNode().getNodeName());
                mapArr.put("nodeIcon", x.getSysNode().getNodeIcon());
                mapArr.put("nodeSort", x.getSysNode().getNodeSort());
                mapArr.put("nodeType", x.getSysNode().getNodeType());
                mapArr.put("nodeDesc", x.getSysNode().getNodeDesc());
                mapArr.put("nodePid", x.getSysNode().getNodePid());
                mapArr.put("nodeUrl", x.getSysNode().getNodeUrl());
                mapArr.put("nodeTopPid", x.getSysNode().getNodeTopPid());
                mapArr.put("roleId", x.getSysRoleNode().getRoleId());
                mapArr.put("level", level);
                mapArr.put("childList", roleNodeChild(x.getSysNode().getNodeId(), menuCommon, ++level));
                list.add(mapArr);
            }
        }
        return list;
    }

    private List<?> roleNodeChild(Integer id, List<SysRoleNodeVo> menuCommon, int level){
        List<Object> lists = new ArrayList<Object>();
        for(SysRoleNodeVo a:menuCommon){
            Map<String,Object> childArray = new LinkedHashMap<String, Object>();
            if(a.getSysNode().getNodePid() == id){
                childArray.put("nodeId", a.getSysNode().getNodeId());
                childArray.put("nodeName", a.getSysNode().getNodeName());
                childArray.put("nodeIcon", a.getSysNode().getNodeIcon());
                childArray.put("nodeSort", a.getSysNode().getNodeSort());
                childArray.put("nodeType", a.getSysNode().getNodeType());
                childArray.put("nodeDesc", a.getSysNode().getNodeDesc());
                childArray.put("nodePid", a.getSysNode().getNodePid());
                childArray.put("nodeUrl", a.getSysNode().getNodeUrl());
                childArray.put("nodeTopPid", a.getSysNode().getNodeTopPid());
                childArray.put("roleId", a.getSysRoleNode().getRoleId());
                childArray.put("level", level);
                childArray.put("childList", roleNodeChild(a.getSysNode().getNodeId(), menuCommon, ++level));
                lists.add(childArray);
            }
        }
        return lists;
    }

    /**
     * 获取左侧菜单的树形结构
     * @param sysNodes
     * @return [
     *  'id'=>'',
     *  'pid'=>'',
     *  'childList'=>[]
     */
    private List<Object> nodeMenuList(List<SysNode> sysNodes){
        List<SysNode> menuCommon = sysNodes;
        List<Object> list = new ArrayList<Object>();
        int level = 1;
        for (SysNode x : sysNodes) {
            Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
            if(x.getNodePid() == 0){
                mapArr.put("nodeId", x.getNodeId());
                mapArr.put("nodeName", x.getNodeName());
                mapArr.put("nodeIcon", x.getNodeIcon());
                mapArr.put("nodeSort", x.getNodeSort());
                mapArr.put("nodeType", x.getNodeType());
                mapArr.put("nodeDesc", x.getNodeDesc());
                mapArr.put("nodePid", x.getNodePid());
                mapArr.put("nodeUrl", x.getNodeUrl());
                mapArr.put("nodeTopPid", x.getNodeTopPid());
                mapArr.put("level", level);
                mapArr.put("childList", nodeMenuChild(x.getNodeId(), menuCommon, ++level));
                list.add(mapArr);
            }
        }
        return list;
    }

    private List<?> nodeMenuChild(Integer id, List<SysNode> menuCommon, int level){
        List<Object> lists = new ArrayList<Object>();
        for(SysNode a:menuCommon){
            Map<String,Object> childArray = new LinkedHashMap<String, Object>();
            if(a.getNodePid() == id){
                childArray.put("nodeId", a.getNodeId());
                childArray.put("nodeName", a.getNodeName());
                childArray.put("nodeIcon", a.getNodeIcon());
                childArray.put("nodeSort", a.getNodeSort());
                childArray.put("nodeType", a.getNodeType());
                childArray.put("nodeDesc", a.getNodeDesc());
                childArray.put("nodePid", a.getNodePid());
                childArray.put("nodeUrl", a.getNodeUrl());
                childArray.put("nodeTopPid", a.getNodeTopPid());
                childArray.put("level", level);
                childArray.put("childList", nodeMenuChild(a.getNodeId(), menuCommon, ++level));
                lists.add(childArray);
            }
        }
        return lists;
    }
}
